<?php
defined('JPATH_BASE') or die();

jimport('joomla.filesystem.file'); 
jimport('joomla.filesystem.folder'); 

class JElementTerm extends JElement
{ 
	
	
	function fetchElement($name, $value, &$node, $control_name)
	{ 
		ob_start();
		?>
		<script type="text/javascript">
			var inputid			=	"<?php echo $control_name.$name; ?>";
			function setTermValue()
			{
				var term_values	=	$(inputid).value;
				term_values = term_values.split(',');
				
				$$('select.termdata').each(function(item){
					item.getElements('option').each(function(opt){
						if(checkArray(term_values, opt.getProperty('value'))){
							opt.selected = true;						
						}
						else
						{
							opt.selected = false;
						}
					});
				});
			}
			function checkArray(arr, obj)
			{
				for(var i=0; i<arr.length; i++) {
					if (arr[i] == obj) return true;
				}
				return false;
			}
			
			function submitbutton(pressbutton)
			{				
				if(pressbutton=='jobs.save'||pressbutton=='jobs.apply')
				{
					var list_term	=	$$('select.termdata');
					var i			=	0;
					var arr_term	=	new Array();
					var	flag	=	1;
					$$('select.termdata').each(function(item){
						var bool		=	0;
						item.getElements('option').each(function(opt){
							if(opt.selected && opt.value!=0)
							{
								arr_term[i]	=	opt.value;
								bool		=	1;
								i++;
							}
												
						});
						if(item.hasClass('required') && bool==0)
						{
							var rel	=	item.getProperty('rel');
							flag=0;
							alert(rel+' is required');													
						}
						
					});					
					if(flag==0)
						return;
					
					var str_term	=	arr_term.join(',');
					
					$(inputid).value=str_term;							
					submitform( pressbutton );		
				}
				submitform( pressbutton );
			}
			
		</script>
			<div id="term"><?= JText::_('please select node type') ?></div>
			<input type="hidden" name="<?= $control_name.'['.$name.']'; ?>" value="<?= $value; ?>" id="<?= $control_name.$name; ?>" value="<?= $value; ?>" />
		<?php		
		$str	=	ob_get_contents();
		ob_end_clean();
		return $str;
	}	
}
